package wechat

import (
	"fmt"
	"github.com/silenceper/wechat/v2"
	"github.com/silenceper/wechat/v2/cache"
	"github.com/silenceper/wechat/v2/miniprogram"
	miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
	"github.com/silenceper/wechat/v2/officialaccount"
	offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
)

type Wechat struct {
	*wechat.Wechat
}

type RedisConfiguration struct {
	Host     string `yaml:"Host"`
	Port     int    `yaml:"Port"`
	Password string `yaml:"Password"`
	Database int    `yaml:"Database"`
}

type MpConfiguration struct {
	AppId          string `yaml:"AppId"`
	AppSecret      string `yaml:"AppSecret"`
	Token          string `yaml:"Token"`
	EncodingAESKey string `yaml:"EncodingAESKey"`
}

type MiniConfiguration struct {
	AppId     string `yaml:"AppId"`
	AppSecret string `yaml:"AppSecret"`
}

func NewWechat(config RedisConfiguration) *Wechat {
	wc := &Wechat{
		wechat.NewWechat(),
	}
	redisCache := cache.NewRedis(&cache.RedisOpts{
		Host:     fmt.Sprintf("%s:%d", config.Host, config.Port),
		Password: config.Password,
		Database: config.Database,
	})
	wc.SetCache(redisCache)
	return wc
}

func (w *Wechat) MpWechat(config MpConfiguration) *officialaccount.OfficialAccount {
	return w.GetOfficialAccount(&offConfig.Config{
		AppID:          config.AppId,
		AppSecret:      config.AppSecret,
		Token:          config.Token,
		EncodingAESKey: config.EncodingAESKey,
	})
}

func (w *Wechat) MiniProgram(config MiniConfiguration) *miniprogram.MiniProgram {
	return w.GetMiniProgram(&miniConfig.Config{
		AppID:     config.AppId,
		AppSecret: config.AppSecret,
	})
}
